using System;
using System.Collections;
using System.Collections.Generic;
using GardeningSkeleton.Platform;
using MyFrame.Pool;
using MyFrame.Single;
using UnityEngine;


public enum ParticleFXType
{
    Fronze,
}

public class EffectMgr : SingletonMonoGlobal<EffectMgr>
{
    /// <summary>
    /// 添加特效到世界, 确保粒子特效和动画特效有相同的行为 
    /// </summary>
    /// <param name="pos">出生位置.</param>
    /// <param name="scale">特效的尺寸.</param>
    /// <returns>返回添加的特效.</returns>
    public GameObject AddWorldEffect(String fxType, Vector3 worldPos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
    {
        var effect = PoolMgr.Instance.GetObj(fxType, worldPos, rotation);
        effect.transform.localScale = Vector3.one * scale;

        StartEffect(effect, duration);

        return effect;
    }
    public GameObject AddWorldEffect(BloodParticleType fXType, Vector3 worldPos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
     => AddWorldEffect("BloodParticle" + fXType.ToString(), worldPos, rotation, scale, duration);
    public GameObject AddWorldEffect(HitEffect hitType, Vector3 worldPos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
     => AddWorldEffect(hitType.ToString(), worldPos, rotation, scale, duration);
    public GameObject AddWorldEffect(ParticleFXType fXType, Vector3 worldPos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
     => AddWorldEffect(fXType.ToString(), worldPos, rotation, scale, duration);

    void StartEffect(GameObject effect, float duration = 0f)
    {
        // 确保只有一个动画器，这是为了动画有父级，可以自定义旋转和缩放
        var animator = effect.GetComponentInChildren<Animator>();
        if (animator != null)
        {
            // 从头开始播放
            animator.Play(0);
            StartCoroutine(AutoRelaseAnimal(animator));
        }
        else if (effect.TryGetComponent<ParticleSystem>(out var ps))
        {
            ps.Play();

            if (duration > 0f) StartCoroutine(ReleaseAfterDuation(effect, duration));
            else
            {
                StartCoroutine(AutoRelaseParticle(ps));
            }
        }
    }

    private IEnumerator AutoRelaseAnimal(Animator animator)
    {
        // 获取初始动画状态信息
        // 这种方式可以处理循环的动画,更灵活
        // 只要动画还没有播放完毕或者正在进行过渡，就继续等待   normalizedTime 是动画的标准化时间，范围从 0.0 到 1.0，表示动画从开始到结束的相对进度
        while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.95f || animator.IsInTransition(0))
        {
            // 在动画未结束或者在过渡中时继续等待
            yield return null;
        }
        // 等待当前动画片段播放完毕
        // yield return new WaitForSeconds(animator.GetCurrentAnimatorClipInfo(0)[0].clip.length);

        // 释放对象
        PoolMgr.Instance.Release(animator.gameObject);
    }
    private IEnumerator AutoRelaseParticle(ParticleSystem ps)
    {
        // 等待粒子系统播放结束
        // yield return new  WaitForSeconds(ps.main.duration);
        while (ps.isPlaying)
        {
            yield return null; 
        }
        // 释放对象
        PoolMgr.Instance.Release(ps.gameObject);
    }

    /// <summary>
    /// 添加特效到指定物体
    /// </summary>
    /// <param name="effectid">配置表中的id.</param>
    /// <param name="obj">跟随物体，如果此物体不为空，特效会跟随此体移动，直到父物体变为null.</param>
    /// <param name="pos">特效在obj中的相对坐标(偏移).</param>
    /// <param name="scale">特效的尺寸.</param>
    /// <returns>返回添加的特效.</returns>
    public GameObject AddEffect(String fxType, Transform parent, Vector3 pos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
    {
        var effect = PoolMgr.Instance.GetObj(fxType.ToString());
        effect.transform.SetParent(parent);
        effect.transform.localPosition = pos;
        effect.transform.localRotation = rotation;
        effect.transform.localScale = Vector3.one * scale;

        StartEffect(effect, duration);
        return effect;
    }
    public GameObject AddEffect(HitEffect hitType, Transform parent, Vector3 pos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
    => AddEffect(hitType.ToString(), parent, pos, rotation, scale, duration);
    public GameObject AddEffect(ParticleFXType fXType, Transform parent, Vector3 pos = default, Quaternion rotation = default, float scale = 1f, float duration = 0f)
     => AddEffect(fXType.ToString(), parent, pos, rotation, scale, duration);




    private IEnumerator ReleaseAfterDuation(GameObject effect, float duration)
    {
        yield return new WaitForSeconds(duration);

        // 确保粒子播放结束后归还对象池
        PoolMgr.Instance.Release(effect);
    }

    public void Release(GameObject obj)
    {
        PoolMgr.Instance.Release(obj);
    }
}
